package cn.com.flat.utils;



import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Properties;

/**
 * 标题: 数据库配置文件
 * <p>
 * 描述: 获取classpath下数据库配置文件信息
 * <p>
 * 版权：Copyright (c) 2016
 * <p>
 *
 * @author 张顺
 * @version 1.0
 * @created 2016/11/09/23:30
 */
public class DbConfig {

    private static Logger logger = LoggerFactory.getLogger(DbConfig.class);

    private static Properties properties = null;

    static{
        properties = new Properties();
        try {
            URL url = IpmanConfig.class.getClassLoader().getResource("db/db.properties");
            File file = new File(url.getFile());
            InputStream inputStream = new FileInputStream(file);
            properties.load(inputStream);
        } catch (Exception e) {
            logger.error("DbConfig load error",e);
        }
    }

    /**
     * 根据key获取配置文件value
     * @param key
     * @return
     */
    public static String getProperty(String key){
        return getProperty(key,"");
    }

    /**
     * 如果根据key没有找到value,则填充defaultValue
     * @param key
     * @param defaultValue
     * @return
     */
    public static String getProperty(String key,String defaultValue){
        return properties.getProperty(key,defaultValue);
    }
}
